#include "WorldMapCacheData.h"

WorldMapCacheData* WorldMapCacheData::mInstance = NULL;

void WorldMapCacheData::CreateInstance()
{
	if (mInstance == nullptr)
	{
		mInstance = new WorldMapCacheData();
	}
}

WorldMapCacheData* WorldMapCacheData::GetInstance()
{
	return mInstance;
}

WorldMapCacheData::WorldMapCacheData()
	: m_maxCount(0)
{
}

WorldMapCacheData::~WorldMapCacheData()
{
	Clear();
}

bool WorldMapCacheData::Reload()
{
	_safe_guard_begin;

	Clear();

	Initialization();

	_safe_guard_end;

	return false;
}

void WorldMapCacheData::Clear()
{
	_safe_guard_begin;

	auto itor = m_mapCacheData.begin();
	for (; itor != m_mapCacheData.end(); ++itor)
	{
		_safe_delete(itor->second);
	}

	m_mapCacheData.clear();

	_safe_guard_end;
}

bool WorldMapCacheData::Initialization()
{
	_safe_guard_begin;

	Json::Value _jv = ReadJsonFile("./data/map/worldMap.json");
	if (_jv == Json::nullValue)
	{
		LOG_ERROR("default","ReadJsonFile -> data/map/worldMap.json failed ......");
		return false;
	}

	m_maxCount = _jv.size();

	Json::Value::Members _tmp = _jv.getMemberNames();
	uint32_t _size = (uint32_t)(_tmp.size());
	for (uint32_t i = 0; i < _size; ++i)
	{
		uint32_t _idx = (uint32_t)atoi(_tmp[i].c_str());

		AddData(_idx, _jv[_tmp[i]]);
	}

	return true;

	_safe_guard_end;

	return false;
}

void WorldMapCacheData::AddData(uint32_t id, Json::Value& jm)
{
	_safe_guard_begin;

	auto itor = m_mapCacheData.find(id);
	if (itor == m_mapCacheData.end())
	{
		WorldMap* p = new WorldMap;
		_safe_check(p);

		p->mId = jm["bigmap_id"].asUInt();
		strcpy(p->mName, jm["name"].asString().c_str());
		strcpy(p->mMapName, jm["map_name"].asString().c_str());

		Json::Value ordinary = jm["ordinary"];
		uint32_t sz = ordinary.size();
		for (uint32_t i = 0; i < sz; ++i)
		{
			p->mOrdinary.push_back(ordinary[i].asUInt());
		}

		Json::Value elite = jm["elite"];
		sz = elite.size();
		for (uint32_t i = 0; i < sz; ++i)
		{
			p->mElite.push_back(elite[i].asUInt());
		}

		p->mIsOpened = jm["is_opened"].asUInt();


		// [[103000100,1,30],[103000200,1,10],[106000600,1,30]]
		Json::Value ordinaryReward = jm["ordinary_reward"];
		sz = ordinaryReward.size();
		for (uint32_t i = 0; i < sz; ++i)
		{
			Json::Value vv = ordinaryReward[i];
			uint32_t szz = vv.size();
			UInt32_6x c;
			for (uint32_t n = 0; n < szz; ++n)
			{
				c.mValue[n] = vv[n].asUInt();
			}

			p->mOrdinaryReward.push_back(c);
		}

		// [[103000100,1,30],[103000200,1,10],[106000600,1,30]]
		Json::Value eliteReward = jm["elite_reward"];
		sz = eliteReward.size();
		for (uint32_t i = 0; i < sz; ++i)
		{
			Json::Value vv = eliteReward[i];
			uint32_t szz = vv.size();
			UInt32_6x c;
			for (uint32_t n = 0; n < szz; ++n)
			{
				c.mValue[n] = vv[n].asUInt();
			}

			p->mEliteReward.push_back(c);
		}

		m_mapCacheData[id] = p;
	}

	_safe_guard_end;
}

WorldMap* WorldMapCacheData::GetData(uint32_t id)
{
	_safe_guard_begin;

	auto itor = m_mapCacheData.find(id);
	if (itor != m_mapCacheData.end())
	{
		return itor->second;
	}
	_safe_guard_end;

	return nullptr;
}

bool WorldMapCacheData::GetDataJsonString(uint32_t id, Json::Value& _jm)
{
	_safe_guard_begin;

	WorldMap* p = GetData(id);
	if (nullptr == p)
	{
		LOG_ERROR("default","Error19");
		return false;
	}

	_jm.clear();

	_jm["id"] = Json::Value(p->mId);
	_jm["name"] = Json::Value(p->mName);
	_jm["map_name"] = Json::Value(p->mMapName);

	// [0,0,0,0,0]
	Json::Value ordinary;
	for (auto it = p->mOrdinary.begin(); it != p->mOrdinary.end(); ++it)
	{
		if ((*it))
		{
			ordinary.append((*it));
		}
	}
	_jm["ordinary"] = ordinary;

	Json::Value elite;
	for (auto it = p->mOrdinary.begin(); it != p->mOrdinary.end(); ++it)
	{
		if ((*it))
		{
			elite.append((*it));
		}
	}
	_jm["elite"] = elite;

	Json::Value ordinaryReward;
	for (auto it = p->mOrdinaryReward.begin(); it != p->mOrdinaryReward.end(); ++it)
	{
		Json::Value tmp;
		if ((*it).mValue)
		{
			int len = sizeof((*it).mValue) / sizeof((*it).mValue[0]);
			for (int i = 0; i < len; ++i)
			{
				if ((*it).mValue[i])
				{
					tmp.append((*it).mValue[i]);
				}
			}

			ordinaryReward.append(tmp);
		}
	}
	_jm["ordinary_reward"] = ordinaryReward;

	_safe_guard_end;

	return true;
}

uint32_t WorldMapCacheData::GetDataCount()
{
	_safe_guard_begin;

	return m_maxCount;

	_safe_guard_end;
}